package com.pn.controller;


import com.pn.entity.*;
import com.pn.page.Page;
import com.pn.service.*;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private StoreService storeService;
    @Resource
    private BrandService brandService;
    @Resource
    private ProductTypeService productTypeService;
    @Resource
    private SupplyService supplyService;
    @Resource
    private PlaceService placeService;
    @Resource
    private UnitService unitService;
    @Resource
    private ProductService productService;
    @Value("${file.upload-path}")
    private String uploadClassPath;
    @Resource
    private TokenUtils tokenUtils;

    //查询所有仓库
    @RequestMapping("/store-list")
    public Result findAllStore(){
        List<Store> allStore = storeService.findAllStore();
        return Result.ok(allStore);
    }
    //查询所有品牌
    @RequestMapping("/brand-list")
    public Result findAllBrand(){
        List<Brand> allBrand = brandService.findAllBrand();
        return Result.ok(allBrand);
    }

    //查询所有产品分类树
    @RequestMapping("/category-tree")
    public Result findProductTypeTree(){
        List<ProductType> productTypeTree = productTypeService.findProductTypeTree();
        return Result.ok(productTypeTree);
    }

    //查询所有供应商
    @RequestMapping("/supply-list")
    public Result findSupply(){
        List<Supply> supplyList = supplyService.findAllSupply();
        return Result.ok(supplyList);
    }

    //查询所有产地
    @RequestMapping("/place-list")
    public Result findAllPlace(){
        List<Place> placeList = placeService.findAllPlace();
        return Result.ok(placeList);
    }

    //查询所有单位
    @RequestMapping("/unit-list")
    public Result findAllUnit(){
        List<Unit> unitList = unitService.findAllUnit();
        return Result.ok(unitList);
    }

    //分页查询产品
    @RequestMapping("/product-page-list")
    public Result findProductByPage(Product product, Page page){
        Page resultPage = productService.findAllProductByPage(product, page);
        return Result.ok(resultPage);
    }

    //上传图片
//    @CrossOrigin
//    @RequestMapping("/img-upload")
//    public Result uploadImage(MultipartFile file){
//        try {
//            File uploadDirFile = ResourceUtils.getFile(uploadClassPath);
//            String uploadDirPath = uploadDirFile.getAbsolutePath();
//            String filename = file.getOriginalFilename();
//            String uploadImagePath = uploadDirPath + "\\" + filename;
//            file.transferTo(new File(uploadImagePath));
//            return Result.ok("图片上传成功！");
//        } catch (Exception e) {
//            return Result.err(Result.CODE_ERR_BUSINESS,"图片上传失败！");
//        }
//    }
    @CrossOrigin
    @RequestMapping("/img-upload")
    public Result uploadImage(MultipartFile file){
        try {
            String filename = file.getOriginalFilename();
            ApplicationHome applicationHome = new ApplicationHome(this.getClass());
            String path = applicationHome.getDir().getParentFile().getParentFile()
                    + "\\src\\main\\resources\\static\\img\\upload\\" + filename;
            file.transferTo(new File(path));
            return Result.ok("图片上传成功！");
        } catch (Exception e) {
            return Result.err(Result.CODE_ERR_BUSINESS,"图片上传失败！");
        }
    }

    //添加商品
    @RequestMapping("/product-add")
    public Result addProduct(@RequestBody Product product,@RequestHeader(value = WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();
        product.setCreateBy(userId);
        Result result = productService.addProduct(product);
        return result;
    }

    //修改上下架状态
    @RequestMapping("/state-change")
    public Result modifyProductState(@RequestBody Product product){
        int result = productService.modifyStateByPid(product);
        if (result > 0){
            return Result.ok("上下架状态修改成功！");
        }else {
            return Result.err(Result.CODE_ERR_BUSINESS,"上下架状态修改失败！");
        }
    }

    //删除单个商品
    @RequestMapping("/product-delete/{pid}")
    public Result removeProductByPid(@PathVariable Integer pid){
        Integer[] pids = {pid};
        int result = productService.removeProduct(pids);
        if (result > 0){
            return Result.ok("商品删除成功！");
        }else {
            return Result.err(Result.CODE_ERR_BUSINESS,"商品删除失败！");
        }
    }

    //批量删除商品
    @RequestMapping("/product-list-delete")
    public Result removeBatchProduct(@RequestBody Integer[] pid){
        int result = productService.removeProduct(pid);
        if (result > 0){
            return Result.ok("商品删除成功！");
        }else {
            return Result.err(Result.CODE_ERR_BUSINESS,"商品删除失败！");
        }
    }

    //修改商品信息
    @RequestMapping("/product-update")
    public Result modifyProduct(@RequestBody Product product,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){

        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int userId = currentUser.getUserId();
        product.setUpdateBy(userId);
        Result result = productService.modifyProduct(product);
        return result;
    }









}
